﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class OVRUIFrame : IUIFrame
{
    public const string ANCHOR_NAME = "RightHandAnchor";
    public const float CANVAS_SCALE = 0.00175f;
    public readonly static Vector3 CANVAS_DIR = new Vector3(0, -0.35f, 1.09f);
    private GameObject vrui;
    public GameObject InitEnviroment(GameObject worldCamera)
    {
        UnityEngine.Object canvasPrefab = Resources.Load("Prefab/UI/OVRUI");
        vrui = GameObject.Instantiate(canvasPrefab) as GameObject;
        GameObject canvas = vrui.transform.Find("OVRCanvas").gameObject;
        GameObject es = vrui.transform.Find("EventSystem").gameObject;
        GameObject point = vrui.transform.Find("GazePointerRing").gameObject;

        //赋值相关属性
        Transform rayTrans = worldCamera.transform.Find("TrackingSpace/" + ANCHOR_NAME);
        es.GetComponent<OVRInputModule>().rayTransform = rayTrans;
        point.GetComponent<OVRGazePointer>().rayTransform = rayTrans;
        canvas.GetComponent<Canvas>().worldCamera = worldCamera.transform.Find("TrackingSpace/CenterEyeAnchor").GetComponent<Camera>();

        return canvas; 
    }

    public void ReleaseEnviroment()
    {
        GameObject.Destroy(vrui);
    }

    public void UpdateCanvasPosition(RectTransform rt, Transform playerTransform)
    {
        //初始化canvas位置
        Vector3 forward = playerTransform.forward; 
        forward.y = 0;
        Vector3 pos = playerTransform.position;
        pos += forward * CANVAS_DIR.z;
        pos.y += CANVAS_DIR.y;
        rt.position = pos;
    }

}
